
/**
 * Created by 愚人码头 .
 * User: 愚人码头
 * Date: 11-5-19
 * Time: 上午10:24
 * 更多查看http://www.css88.com/archives/3627
 */
//在光标位置插入内容
(function($) {
    $.fn.extend({
    	getCurPos: function(){
	        var e=$(this).get(0);
	        e.focus();
	        if(e.selectionStart){    //FF
	            return e.selectionStart;
	        }
	        if(document.selection){    //IE
	            var r = document.selection.createRange();
	            if (r == null) {
	                return e.value.length;
	            }
	            var re = e.createTextRange();
	            var rc = re.duplicate();
	            re.moveToBookmark(r.getBookmark());
	            rc.setEndPoint('EndToStart', re);
	            return rc.text.length;
	        }
	        return e.value.length;
	    },
	    setCurPos: function(pos) {
	        var e=$(this).get(0);
	        e.focus();
	        if (e.setSelectionRange) {
	            e.setSelectionRange(pos, pos);
	        } else if (e.createTextRange) {
	            var range = e.createTextRange();
	            range.collapse(true);
	            range.moveEnd('character', pos);
	            range.moveStart('character', pos);
	            range.select();
	        }
	    },       
    	selectContent: function(s) {
    		var id = $(this).attr("id");
    	
	         var t = document.getElementById(id), v = t.value, start = v.indexOf(s), end;
	         if (start == -1) return//找不到内容则推出
	         end = start + s.length;
	         
	         if (typeof t.createTextRange != 'undefined') { //IE
	             var r = t.createTextRange();
	             //先将光标重合
	             r.moveStart('character', 0);
	             r.moveEnd('character', 0);
	             r.collapse(true);
	             r.moveEnd('character', end);
	             r.moveStart('character', start);
	             r.select();
	         } else if (typeof t.selectionStart!='undefined') { //firefox,chrome
	             t.selectionStart = start;
	             t.selectionEnd=end
	         }
	     },
        insertContent: function(myValue, t) {
            var $t = $(this)[0];
            if (document.selection) { //ie
                this.focus();
                var sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
                sel.moveStart('character', -l);
                var wee = sel.text.length;
                if (arguments.length == 2) {
                    var l = $t.value.length;
                    sel.moveEnd("character", wee + t);
                    if (t <= 0){
                    	sel.moveStart("character", wee - 2 * t - myValue.length)
                    } else {
                    	sel.moveStart("character", wee - t - myValue.length);
                    }
                    sel.select();
                }
            } else if ($t.selectionStart || $t.selectionStart == '0') {
                var startPos = $t.selectionStart;
                var endPos = $t.selectionEnd;
                var scrollTop = $t.scrollTop;
                $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
                this.focus();
                $t.selectionStart = startPos + myValue.length;
                $t.selectionEnd = startPos + myValue.length;
                $t.scrollTop = scrollTop;
                if (arguments.length == 2) {
                    $t.setSelectionRange(startPos - t, $t.selectionEnd + t);
                    this.focus();
                }
            }
            else {
                this.value += myValue;
                this.focus();
            }
        }
    })
})(jQuery);

//$(文本域选择器).insertContent("插入的内容");
//$(文本域选择器).insertContent("插入的内容"，数值); //根据数值选中插入文本内容两边的边界
